﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using FrameworkLib.Filters;

namespace ControlsLib.Filters
{
    public partial class FilterContainerBase<T> : UserControl, IFilterContainerBase<T> where T : new()
    {
        public event EventHandler ExecuteFilter;
        public event EventHandler ResetFilter;
        public event EventHandler RefreshFilterData;

        public T FilterBag
        {
            get { return new T(); }
        }

        public DateTime? FromDate
        {
            get { return this.dtpFromDate.Value; }
            set { this.dtpFromDate.Value = value.GetValueOrDefault(); }
        }

        public DateTime? ToDate
        {
            get { return this.dtpToDate.Value; }
            set { this.dtpToDate.Value = value.GetValueOrDefault(); }
        }

        public object PopupContainerControl
        {
            get;
            set;
        }

        public FrameworkLib.Filters.FilterControl ControlManager
        {
            get;
            set;
        }

        public FilterContainerBase()
        {
            InitializeComponent();

            this.btnFilter.Click += new EventHandler(btnFilter_Click);
            this.checkBox1.CheckedChanged += new EventHandler(checkBox1_CheckedChanged);

            this.checkBox1.Checked = true;
        }

        void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            ManageCheckGroupBox(this.checkBox1, this.groupBox1);
        }

        void btnFilter_Click(object sender, EventArgs e)
        {
            if (ExecuteFilter != null)
            {
                ExecuteFilter(this, EventArgs.Empty);
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            this.dtpFromDate.Value = DateTime.Now;
            this.dtpToDate.Value = DateTime.Now;
        }

        private void ManageCheckGroupBox(CheckBox chk, GroupBox grp)
        {
            // Make sure the CheckBox isn't in the GroupBox.
            // This will only happen the first time.
            if (chk.Parent == grp)
            {
                // Reparent the CheckBox so it's not in the GroupBox.
                grp.Parent.Controls.Add(chk);

                // Adjust the CheckBox's location.
                chk.Location = new Point(
                    chk.Left + grp.Left,
                    chk.Top + grp.Top);

                // Move the CheckBox to the top of the stacking order.
                chk.BringToFront();
            }

            // Enable or disable the GroupBox.
            grp.Enabled = (chk.Checked);

            // Get the appropriate color for the contained controls.
            Color fore_color;
            if (grp.Enabled)
            {
                fore_color = Color.FromKnownColor(KnownColor.ActiveCaptionText);
            }
            else
            {
                fore_color = Color.FromKnownColor(KnownColor.InactiveCaptionText);
            }

            // Color the controls in the GroupBox.
            foreach (Control ctl in grp.Controls)
            {
                ctl.ForeColor = fore_color;
            }
        }


        public void RefreshData()
        {
            
        }
    }
}
